package com.pubinfo.passbook.common.model.http.User;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;

import java.io.Serializable;

@Data
public class UserResult implements Serializable {
  
  private static final long serialVersionUID = 0;
  
  @ApiModelProperty(value = "是否成功")
  private boolean success = true;
  
  @ApiModelProperty(value = "结果码")
  private String resultCode = "SUCCESS";
  /**
   * 状态码对应的消息
   */
  @ApiModelProperty(value = "结果消息")
  private String resultMsg = "ok";
  /**
   * 页面上要具体呈现的数据
   */
  @ApiModelProperty(value = "返回数据")
  private Object result;
  
  public UserResult(boolean success, String resultCode, String resultMsg, Object result) {
    this.success = success;
    this.resultCode = resultCode;
    this.resultMsg = resultMsg;
    this.result = result;
  }
  
  public UserResult(boolean success, String resultCode, String resultMsg) {
    this.success = success;
    this.resultCode = resultCode;
    this.resultMsg = resultMsg;
  }
  
  public UserResult(UserResultCode resultCode) {
    this.success = false;
    this.resultCode = resultCode.getResultCode();
    this.resultMsg = resultCode.getResultMsg();
  }
  
  public static UserResult error(String userResultCode, String message) {
    return new UserResult(false, userResultCode, message);
  }
  
  public static String error(UserResultCode resultCode) {
    return JSONObject.toJSONString(new UserResult(resultCode));
  }
  
  public static String success(Object result) {
    
    return JSONObject.toJSONString(new UserResult(true, UserResultCode.SUCCESS.getResultCode(), UserResultCode.SUCCESS.getResultMsg(), result));
    
  }
}
